package com.module.lemlin.util

import android.app.Activity
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Rect
import android.os.Bundle
import android.os.Handler
import com.blankj.utilcode.util.ToastUtils
import com.module.lemlin.base.BaseApplication
import com.tencent.connect.share.QQShare
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXImageObject
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.tencent.tauth.IUiListener
import com.tencent.tauth.Tencent
import com.tencent.tauth.UiError
import java.io.ByteArrayOutputStream
import java.io.File

/**
 * 微信-分享图片
 * @param mBitmap 位图
 * @param mTargetScene SendMessageToWX.Req.WXSceneSession//分享到好友会话
 *                     SendMessageToWX.Req.WXSceneTimeline//是分享到朋友圈
 * */
fun wxShareImage(
    mBitmap: Bitmap, mTargetScene: Int = SendMessageToWX.Req.WXSceneSession
) {

    if (!isWXAppInstalled)
        return

    val imgObj = WXImageObject(mBitmap)

    val msg = WXMediaMessage()
    msg.mediaObject = imgObj

    val thumbBitmap: Bitmap = Bitmap.createScaledBitmap(
        mBitmap, THUMB_SIZE, THUMB_SIZE, true
    )
    mBitmap.recycle()
//    msg.thumbData = bmpToByteArray(thumbBitmap, needRecycle = true)

    val req = SendMessageToWX.Req()
    req.transaction = buildTransaction("img")
    req.message = msg
    req.scene = mTargetScene

    iwxapi.sendReq(req)

}

/**
 * 微信-分享图片
 * @param file 文件地址
 * @param mTargetScene SendMessageToWX.Req.WXSceneSession//分享到好友会话
 *                     SendMessageToWX.Req.WXSceneTimeline//是分享到朋友圈
 * */
fun wxShareImage(path:String, mTargetScene: Int = SendMessageToWX.Req.WXSceneSession) {

    val bitmap: Bitmap = try {
        BitmapFactory.decodeFile(path)
    } catch (e: Exception) {
        ToastUtils.showShort("非法的图片地址")
        return
    }
    wxShareImage(bitmap, mTargetScene)
}

/**
 * 微信-分享图文链接
 * */
fun wxShareLink(
    mTitle: String, mDesc: String, mUrl: String, mBitmap: Bitmap? = null,
    mTargetScene: Int = SendMessageToWX.Req.WXSceneSession
) {

    if (!isWXAppInstalled)
        return

    val webObj = WXWebpageObject()
    webObj.webpageUrl = mUrl

    val msg = WXMediaMessage(webObj)
    msg.title = mTitle
    msg.description = mDesc
    mBitmap?.let {
        msg.thumbData = bmpToByteArray(it, needRecycle = true)
    }

    val req = SendMessageToWX.Req()
    req.transaction = buildTransaction("web")
    req.message = msg
    req.scene = mTargetScene

    iwxapi.sendReq(req)
}

/**
 * QQ-分享图片
 * @param activity 当前活动
 * @param file 文件地址
 * */
fun qqShareImage(activity: Activity, path: String) {
    if (!isQQAppInstalled)
        return
    val params = Bundle()
    params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE)
    params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, path)
    Handler().post {
        iqqapi.shareToQQ(activity, params, iUiListener)
    }
}


/**
 * QQ-分享图文链接
 * @param activity 当前活动
 * @param title 标题
 * @param desc 摘要
 * @param url 内容地址
 * @param image 图标地址
 * */
fun qqShareLink(
    activity: Activity, title: String, desc: String, url: String, image: String? = null
) {
    if (!isQQAppInstalled)
        return
    val params = Bundle()
    params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT)
    params.putString(QQShare.SHARE_TO_QQ_TITLE, title)
    params.putString(QQShare.SHARE_TO_QQ_SUMMARY, desc)
    params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url)
    image?.let {
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, it)
    }
    Handler().post {
        iqqapi.shareToQQ(activity, params, iUiListener)
    }
}


fun buildTransaction(type: String? = null): String {
    return if (type == null) System.currentTimeMillis()
        .toString() else type + System.currentTimeMillis()
}

fun bmpToByteArray(bmp: Bitmap, needRecycle: Boolean): ByteArray? {
    var i: Int
    var j: Int
    if (bmp.height > bmp.width) {
        i = bmp.width
        j = bmp.width
    } else {
        i = bmp.height
        j = bmp.height
    }
    val localBitmap: Bitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565)
    val localCanvas = Canvas(localBitmap)
    while (true) {
        localCanvas.drawBitmap(bmp, Rect(0, 0, i, j), Rect(0, 0, i, j), null)
        if (needRecycle) bmp.recycle()
        val localByteArrayOutputStream = ByteArrayOutputStream()
        localBitmap.compress(
            Bitmap.CompressFormat.JPEG, 100,
            localByteArrayOutputStream
        )
        localBitmap.recycle()
        val arrayOfByte: ByteArray = localByteArrayOutputStream.toByteArray()
        try {
            localByteArrayOutputStream.close()
            return arrayOfByte
        } catch (e: Exception) {
            //F.out(e);
        }
        i = bmp.height
        j = bmp.height
    }
}